/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Inquiry;
import org.cx1.model.Listing;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IEmailService;
import org.cx1.service.api.IInquiryService;
import org.cx1.service.api.IListingService;

/**
 *
 * @author ECH
 */
public class MInquiryService implements IInquiryService {

    private IEmailService emailService = new MEmailService();
    private IListingService listingService = new MListingService();
    
    @Override
    public ServiceResponse<String> submitTourSpaceInquiry(String name, String email, String phone, String listingID, String listingCreatorID) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Inquiry entry = new Inquiry();
            id = UUID.randomUUID().toString();
            
            entry.setId(id);      
            
            entry.setName(name);
            entry.setEmail(email);
            entry.setPhone(phone);            
            
            entry.setListingID(listingID);
            entry.setListingCreatorID(listingCreatorID);
            
            
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            MockDS.getInquiries().add(entry);
            
            emailService.sendInquiry(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<String> submitTourSpaceInquiry(String name, String email, String phone, String message, String listingID, String listingCreatorID) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String id = null;        
        try {            
            Inquiry entry = new Inquiry();
            id = UUID.randomUUID().toString();
            
            entry.setId(id);      
            
            entry.setName(name);
            entry.setEmail(email);
            entry.setPhone(phone);   
            entry.setMessage(message);
            
            entry.setListingID(listingID);
            entry.setListingCreatorID(listingCreatorID);
            
            
            entry.setActive(true);
            entry.setDateCreated(new Date());
            entry.setDateModified(new Date());
                        
            MockDS.getInquiries().add(entry);
            
            emailService.sendInquiry(entry);
            
            response.setResponse(id);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    
    @Override
    public ServiceResponse<List<Inquiry>> readAllTourSpaceInquiries(int offset, int limit) {
        ServiceResponse<List<Inquiry>> response = new ServiceResponse<List<Inquiry>>();
        List<Inquiry> inquiries = new ArrayList<Inquiry>();
        try {            
            List<Inquiry> records = MockDS.getInquiries();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    inquiries.addAll(records);
                }
                else if (offset < records.size() && limit < records.size()) {
                    inquiries.addAll(records);
                }              
            }            
                      
            response.setResponse(inquiries);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<Inquiry>> readUserTourSpaceInquiries(int offset, int limit, String userID) {
        ServiceResponse<List<Inquiry>> response = new ServiceResponse<List<Inquiry>>();
        List<Inquiry> inquiries = new ArrayList<Inquiry>();
        List<Inquiry> userInquiries = new ArrayList<Inquiry>();
        try {            
            List<Inquiry> records = MockDS.getInquiries();
            if (records != null && records.size() > 0) {
                if (offset == 0 && limit == 0) {
                    inquiries.addAll(records);
                }
                else if (offset < records.size() && limit < records.size()) {
                    inquiries.addAll(records);
                }              
            }            
                      
            //CX: filter user inquiries
            if (inquiries != null && inquiries.size() > 0) {
                for (Inquiry inquiry : inquiries) {
                    if (StringUtils.equalsIgnoreCase(inquiry.getListingCreatorID(), userID)) {
                        userInquiries.add(inquiry);
                    }
                }
            }            
            
            //CX: set listings data
            if (userInquiries != null && userInquiries.size() > 0) {
                for (Inquiry userInquiry : userInquiries) {
                    ServiceResponse<Listing> sr1 = listingService.read(userInquiry.getListingID());
                    if (sr1.isSuccessful() && sr1.getResponse() != null) {
                        Listing listing = sr1.getResponse();
                        userInquiry.setListingTitle(listing.getTitle());
                    }
                }
            }
            
            response.setResponse(userInquiries);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
    
}
